package likou;

import java.util.*;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2021/12/15 15:21
 */
public class LCP_05 {
    public static void main(String[] args) {
        LCP_05 one = new LCP_05();
        System.out.println(Arrays.toString(one.bonus(6, new int[][]{{1, 2}, {1, 6}, {2, 3}, {2, 5}, {1, 4}}, new int[][]{{1, 1, 500}, {2, 2, 50}, {3, 1}, {2, 6, 15}, {3, 1}})));
    }

    class Node {
        int id;
        int val;//当前值
        List<Node> list;

        public Node(int id) {
            this.id = id;
            this.val = 0;
        }

        public boolean add(Node node) {
            if (list == null) {
                list = new ArrayList<>();
            }
            return list.add(node);
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Node node = (Node) o;
            return id == node.id;
        }

        @Override
        public int hashCode() {
            return Objects.hash(id);
        }
    }

    public int[] bonus(int n, int[][] leadership, int[][] operations) {
        Node[] nums = new Node[n + 1];//存储每个节点的父亲
        for (int i = 1; i <= n; i++) {
            nums[i] = new Node(i);
        }
        for (int[] ns : leadership) {
            nums[ns[0]].add(nums[ns[1]]);
        }
        List<Integer> res = new ArrayList<>();
        for (int[] caoz : operations) {
            switch (caoz[0]) {
                case 1:
                    nums[caoz[1]].val += caoz[2];
                    break;
                case 2:
                    if (nums[caoz[1]] == null) {
                        nums[caoz[1]] = new Node(caoz[1]);
                    }
                    nums[caoz[1]].val += caoz[2];
                    List<Node> list = nums[caoz[1]].list;

                    while (list != null && list.size() > 0) {
                        List<Node> temp = new ArrayList<>();
                        for (Node node : list) {
                            if (node.list != null) {
                                temp.addAll(node.list);
                            }
                            node.val += caoz[2];
                        }
                        list = new ArrayList<>(temp);
                    }
                    break;
                case 3:
                    if (nums[caoz[1]] == null) {
                        nums[caoz[1]] = new Node(caoz[1]);
                    }
                    int now = nums[caoz[1]].val;
                    List<Node> list2 = nums[caoz[1]].list;

                    while (list2 != null && list2.size() > 0) {
                        List<Node> temp = new ArrayList<>();
                        for (Node node : list2) {
                            if (node.list != null) {
                                temp.addAll(node.list);
                            }
                            now = (now + node.val) % 1000000007;
                        }
                        list2 = new ArrayList<>(temp);
                    }
                    res.add(now);
                    break;
            }
        }
        int[] resa = new int[res.size()];
        for (int i = 0; i < res.size(); i++) {
            resa[i] = res.get(i);
        }
        return resa;
    }
}
